/*
 * @Description: 
 * @Author: 张凌瑞
 * @Date: 2021-03-06 10:19:08
 * @LastEditTime: 2021-03-08 20:27:20
 * @LastEditors: 张凌瑞
 */

import { Effect, ImmerReducer, Reducer, Subscription } from 'umi';
import { examList  ,examType,classType} from "@/services/exam";


export interface ExamModelState {
  examList: { [propName: string]: any },
  examTypeList: { [propName: string]: any },//考试类型
  classTypeList:{ [propName: string]: any },//课程
  }
  
  export interface ExamModelType {
    namespace: 'exam';
    state: ExamModelState;
    effects: {
      query: Effect;
      examList: Effect;
      examType: Effect,//获取所有考试类型
      classType:Effect,//获取所有的课程
    };
    reducers: {
      save: Reducer<ExamModelState>;
      // 启用 immer 之后
      // save: ImmerReducer<IndexModelState>;
    };
    subscriptions: { setup: Subscription };
  }
  
  const ExamModel: ExamModelType = {
    namespace: 'exam',

    state: {
      examList: [],
      examTypeList: [],
      classTypeList:[],
    },

    effects: {
      *query({ payload }, { call, put }) {},
      *examList({ payload }, { call, put }) {
        let result = yield call(examList);
        if (result.code === 1) {
          // console.log(result);
          yield put({
            type: 'save',
            payload: { examList: result.exam },
          });
        }
      },
      *examType({ payload }, { call, put }) {//获取考试类型
        let result = yield call(examType);
        // console.log(result.data);
        if (result.code === 1) {
          yield put({
            type: 'save',
            payload: { examTypeList: result.data },
          })
        }
      },
      *classType({ payload }, { call, put }) {//获取课程
        let result = yield call(classType);
        if (result.code === 1) {
          yield put({
            type: 'save',
            payload: { classTypeList: result.data },
          })
        }
      },
      
    },
    reducers: {
      save(state, action) {
        return {
          ...state,
          ...action.payload,
        };
      },
      // 启用 immer 之后
      // save(state, action) {
      //   state.name = action.payload;
      // },
    },
    subscriptions: {
      setup({ dispatch, history }) {
        return history.listen(({ pathname }) => {
          if (pathname === '/') {
            dispatch({
              type: 'query',
            });
          }
        });
      },
    },
  };
  
  export default ExamModel;